/* eslint-disable */
const mqtt = require('mqtt');
let { app, should, request, config } = require('./');
const messages = require('../messages');
let robotModel = require('../models/robot');
let bookAlbumModel = require('../models/book_album');
let soundVoiceModel = require('../models/sound_voice');
describe('robot/client', function () {
  let code = '11111111'
  let robot = null;
  let robot_result = null;
  let client = null;
  let server = null;
  let sound_voice_id = null;
  let book_album_id = null;
  let p = null;
  before(function* () {
    robot = yield robotModel.add({ code });
    let sound_voice = yield soundVoiceModel.add({
      album_id: 1,
      title: "test",
      duration: 30,
      attach: "http://",
      order: 1,
    });
    sound_voice_id = sound_voice.id;
    let book_album = yield bookAlbumModel.add({
      title: "test",
      cover: "http://",
      author: "test",
      number: "123123123",
      buy_link: "http://"
    });
    book_album_id = book_album.id;

    server = messages.mqttListen();
  });
  after(function* () {
    let result = yield robotModel.model.deleteByQuery({
      where: [
        ['id', '=', robot.id]
      ]
    });
    yield soundVoiceModel.model.deleteByQuery({
      where: [
        ['id', '=', sound_voice_id]
      ]
    });
    yield bookAlbumModel.model.deleteByQuery({
      where: [
        ['id', '=', book_album_id]
      ]
    });
  });
  it('register', function* () {
    let result = yield request.post('/robot/auth/token')
      .set('Accept', 'application/json')
      .send({ code: robot.code });
    let body = result.body;
    body.should.have.keys('status');
    body.should.have.keys('result');
    body.status.should.be.an.Integer;

    robot_result = body.result;

    // 根据注册接口返回的账号进行mqtt登入
    p = new Promise((resolve, reject) => {
      client = mqtt.connect({
        host: config.mqtt.host,
        port: config.mqtt.port,
        username: robot_result.mqtt_username,
        password: robot_result.mqtt_password,
      });
      client.on('connect', () => {
        console.log("Mqtt robot connect success");
        // mqtt链接成功后，订阅机器人topic，服务器发送测试消息
        client.subscribe(`robot/${robot_result.id}`, () => {
          console.log("Mqtt robot subscribe:" + `robot/${robot_result.id}`);
          server.publish(`robot/${robot_result.id}`, JSON.stringify({
            event: 'test',
            message: 'test message',
          }), console.log);
        });
      });

      // 处理消息，收到消息后退出
      client.on('message', (topic, message) => {
        let info = JSON.parse(message.toString());
        console.log(info);
        client.end();
        server.end();
        resolve();
      });
    })

  });
  it('refresh', function* () {
    let result = yield request.post('/robot/auth/refresh_token')
      .set('Accept', 'application/json')
      .set('token', robot_result.token)
      .send({
        refresh_token: robot_result.refresh_token
      })
    let body = result.body;
    body.should.have.keys('status');
    body.should.have.keys('result');
    body.status.should.be.an.Integer;

    robot_result = body.result;

  });
  it('sound/voice', function* () {
    let result = yield request.get(`/robot/sound/voice/${sound_voice_id}`)
      .set('Accept', 'application/json')
      .set('token', robot_result.token)
      .end();
    let body = result.body;
    body.should.have.keys('status');
    body.should.have.keys('result');
    body.status.should.be.an.Integer;
  });
  it('book/album', function* () {
    let result = yield request.get(`/robot/book/album/${book_album_id}`)
      .set('Accept', 'application/json')
      .set('token', robot_result.token)
      .end();
    let body = result.body;
    body.should.have.keys('status');
    body.should.have.keys('result');
    body.status.should.be.an.Integer;
  });
  it('base/night', function* () {
    let result = yield request.get(`/robot/base/night`)
      .set('Accept', 'application/json')
      .set('token', robot_result.token)
      .end();
    let body = result.body;
    body.should.have.keys('status');
    body.should.have.keys('result');
    body.status.should.be.an.Integer;
  });
  it('base/anti_addiction', function* () {
    let result = yield request.get(`/robot/base/anti_addiction`)
      .set('Accept', 'application/json')
      .set('token', robot_result.token)
      .end();
    let body = result.body;
    body.should.have.keys('status');
    body.should.have.keys('result');
    body.status.should.be.an.Integer;
  });
  it('notify/spell', function* () {
    let result = yield request.put('/robot/notify/spell')
      .set('Accept', 'application/json')
      .set('token', robot_result.token)
      .send({
        module: 'letter',
        module_id: '1',
        duration: 30,
        percent: 0,
      })
    let body = result.body;
    body.should.have.keys('status');
    body.should.have.keys('result');
    body.status.should.be.an.Integer;
  });
  it('message', function* () {
    yield p;
  });
});
